package com.hzpd.hainan.module.video;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzpd.hainan.R;
import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.BaseFragment;
import com.hzpd.hainan.model.video.VideoItemBean;
import com.hzpd.hainan.model.video.VideoItenEntity;
import com.hzpd.hainan.module.video.adapter.VideoRecycleAdapter;
import com.hzpd.hainan.module.video.utils.VideoPlayerHelper;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.PageCtrl;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/7/20.
 */

public class VideoFragmentNew extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.recycler_list_id)
    RecyclerView recyclerListId;
    @BindView(R.id.swipe_id)
    SwipeRefreshLayout swipeId;
    private VideoRecycleAdapter adapter;

    private int page=1;
    private static final int pageSize=15;//
    private boolean mFlagRefresh=true;

    public static VideoFragmentNew newInstance() {
        VideoFragmentNew videoFragment = new VideoFragmentNew();
        return videoFragment;
    }
    @Override
    public int setMyContentView() {
        return R.layout.fragment_video_main;
    }
    @Override
    public void initView() {
        //初始化swip

        swipeId.setOnRefreshListener(this);
        swipeId.setColorSchemeColors(Color.parseColor("#4395DB"));
        final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerListId.setLayoutManager(layoutManager);
        initAdapter();
    }
    /**
     * adapter init
     */
    private void initAdapter() {
        adapter = new VideoRecycleAdapter(null);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this,recyclerListId);
        recyclerListId.setAdapter(adapter);
    }
    @Override
    public void initData() {
        //首次进入 延迟刷新
        swipeId.postDelayed(new Runnable() {
            @Override
            public void run() {
                mFlagRefresh = true;
                getVideoListData();
            }
        }, 200);
    }
    /**
     * 获取视频列表数据
     */
    private void getVideoListData() {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("Page", page+"");
        map.put("PageSize", pageSize+"");
//        map.put("PageSize", pageSize+"");
        Factory.provideHttpService().videoList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<VideoItenEntity>() {
                    @Override
                    public void call(VideoItenEntity entity) {
                        setListData(entity.data);
                        if (mFlagRefresh){
                            LogUtils.i("200-->saveAll");
                            DataSupport.deleteAll(VideoItemBean.class);
                            DataSupport.saveAll(entity.data);
                        }
                        swipeId.setRefreshing(false);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (mFlagRefresh){
                            List<VideoItemBean> list = DataSupport.findAll(VideoItemBean.class);
                            setListData(list);
                        }
                        LogUtils.i("异常-->"+throwable.toString());
                        adapter.loadMoreFail();
                    }
                });
    }
    public void setListData(List<VideoItemBean> list) {

        if (mFlagRefresh){
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        }else{
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->"+adapter.getData().size());
        if (list.size()<pageSize){
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
        swipeId.setEnabled(true);


    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            VideoPlayerHelper.getInstance().stop();
        }
    }


    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        //
        page=1;
        mFlagRefresh = true;
        getVideoListData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        VideoItemBean itemBean = (VideoItemBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.rl_id:
                VideoPlayerHelper.getInstance().play((ViewGroup) view,itemBean.getVideourl(),position);
                break;
            case R.id.video_info_rl:
                VideoPlayerHelper.getInstance().stop();
                PageCtrl.start2VideoPlayActivity(activity,"videoItemFragment",itemBean);
                break;
        }
    }


    private int mCurrentCounter = 0;
    private static final int TOTAL_COUNTER = 18;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    @Override
    public void onLoadMoreRequested() {
        swipeId.setEnabled(false);
        mFlagRefresh=false;
        page++;
        getVideoListData();
    }
}
